package com.endoc.phtotapplication.utils;

import android.graphics.Bitmap;
import android.graphics.Matrix;

import java.io.ByteArrayOutputStream;

/**
 * @author xukang6
 * @data on   2018/5/31 10:45
 * @describe New Class
 * @org HIKVISION
 **/
public class ComPressBitmapUtils {
    /**
     * 按照指定宽高比例缩放，返回压缩后的字节字符串
     * @param bgimage  原图片
     * @param newWidth  指定宽
     * @param newHeight 指定高
     * @return 压缩后的字节字符串
     */
    public static  byte[] zoomImage(Bitmap bgimage, double newWidth, double newHeight) {
        Bitmap bitmap = null;
        byte[] buffer = null;
        float width = bgimage.getWidth();
        float height = bgimage.getHeight();
        Matrix matrix = new Matrix();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        matrix.postScale(scaleWidth, scaleHeight);
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
             bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width,
                    (int) height, matrix, true);
            bitmap.compress(Bitmap.CompressFormat.JPEG,90,baos);
            baos.flush();
            baos.close();
            buffer = baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return buffer;
    }
}
